﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using NDF.Web.Mvc;
using NDF.Web.Utilities;
using UA.Framework.Views;
using UA.Framework.Authorization;

namespace UA.Web.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 系统首页
        /// </summary>
        /// <returns></returns>
        [AllowAuthorize]
        public ActionResult Index()
        {
            return this.View();
        }

        /// <summary>
        /// 系统主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Main()
        {
            MainView model = new MainView();
            return this.View(model);
        }





        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        [AllowAuthorize]
        public ActionResult Login()
        {
            LoginView model = UAAuthentication.LoginViewCookie;
            return model == null ? this.View() : this.View(model);
        }


        /// <summary>
        /// 验证登录请求
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [AllowAuthorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginView model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                int? expirs = model.RememberMe ? new Nullable<int>(10080) : null;
                if (UAAuthentication.UALogin(model.LoginCode, model.Password, expirs))
                {
                    return this.RedirectToLoginSuccess();
                }
                else
                {
                    return this.RedirectToLoginError();
                }
            }
            this.ModelState.AddModelError("LoginErrorMessage", "验证登录请求出错，登录请求数据格式不正确！");
            return this.View(model);
        }



        /// <summary>
        /// 注销登录，并根据请求类型判断返回 JSON 数据或返回到登录页面
        /// </summary>
        /// <returns></returns>
        [AllowAuthorize]
        public ActionResult SignOut()
        {
            UAAuthentication.SignOut();
            if (this.Request.IsAjaxRequest())
            {
                return this.JsonStatus(true, "注销成功", this.Url.Action("Index", "Home"));
            }
            return this.RedirectToAction("Index", "Home");
        }





        public ActionResult Welcome()
        {
            return this.View();
        }




        /// <summary>
        /// 当登录请求验证成功后执行的动作，跳转到系统主页面。
        /// </summary>
        /// <returns></returns>
        protected ActionResult RedirectToLoginSuccess()
        {
            string returnUrl = this.Request["returnUrl"],
                target = string.IsNullOrWhiteSpace(returnUrl) ? this.Url.Action("Main", "Home") : returnUrl;
            if (this.Request.IsAjaxRequest())
            {
                return this.JsonStatus(true, "登录成功", target);
            }
            return this.Redirect(target);
        }

        /// <summary>
        /// 当登录请求验证失败后执行的动作，根据请求类型判断返回 JSON 数据会返回到登录页面
        /// </summary>
        /// <returns></returns>
        protected ActionResult RedirectToLoginError()
        {
            if (this.Request.IsAjaxRequest())
            {
                return this.JsonStatus(false, "登录失败，用户名或密码错误。", this.Url.Action("Index"));
            }
            else
            {
                this.ViewBag.Message = "登录失败，用户名或密码错误！";
                return this.View();
            }
        }


    }
}
